<?php
/* 
 * Auther Bachue
 * This file contains one function updateAuthzSvnAccessFile, if you call the function,
 * updateAuthzSvnAccessFile will read Your database(groups(groupname,username),privileges(reponame,path,privilege,name))
 * and then write your AuthzSvnAccessFile in the sepecify form.
 * you can create the required database by the sql statements as below:
 *
 * create table `groups`(`groupname` varchar(30) not null,`username` varchar(30) not null);
 * create table `privileges`(`reponame` varchar(30) not null,`path` varchar(30) not null,`privilege` varchar(2) not null,`name` varchar(30) not null);
 * alter table `groups` add constraint groups_uni unique(`groupname`,`username`);
 * alter table `privileges` add constraint privileges_uni unique(`reponame`,`path`,`privilege`,`name`);
 *
 * primary key is not necessary.
 */

include_once './config/config.php';
include_once './config/dbconf.php';

function updateAuthzSvnAccessFile()
{
    global $config;
    global $dbconf;
    
    $file_content='';
    $groups_info=array();
    $privileges_info=array();

    $connect=getPdoConnection();
    if(!$connect)
    {
        echo "Database Connection Error";
        //TODO log
        exit;
    }
    
    $file=fopen($config['AuthzSvnAccessFilePath'], 'w');//open file here
    if(!$file)
    {
        echo "File System Error";
        //TODO log
        exit;
    }
    if(flock($file, LOCK_EX)===false)
    {
        echo 'File System Busy';
        //TODO log
        exit;
    }

    $groups_columns=$connect->query(
            "select `{$dbconf['groups_groupname_columnname']}`,`{$dbconf['groups_username_columnname']}`
             from `{$dbconf['groups_tablename']}`");//get all records from groups
    if($groups_columns===false)
    {
        echo 'Database Error';
        //TODO log
        exit;
    }
    $privileges_columns=$connect->query(
            "select `{$dbconf['privileges_reponame_columnname']}`,`{$dbconf['privileges_path_columnname']}`,
                    `{$dbconf['privileges_privilege_columnname']}`,`{$dbconf['privileges_name_columnname']}`
             from `{$dbconf['privileges_tablename']}`");//get all records from privileges;
    if($privileges_columns===false)
    {
        echo 'Database Error';
        //TODO log
        exit;
    }
    $connect=null;//cut the database connection right now!

    foreach($groups_columns as $column)
    {
        $groups_info[$column[$dbconf['groups_groupname_columnname']]][]=$column[$dbconf['groups_username_columnname']];
        //$groups_info is a php array, its form likes
        //$groups_info [$groupname] [$username]
    }

    foreach($privileges_columns as $column)
    {
        $privileges_info[$column[$dbconf['privileges_reponame_columnname']]][$column[$dbconf['privileges_path_columnname']]][$column[$dbconf['privileges_privilege_columnname']]][]=$column[$dbconf['privileges_name_columnname']];
        //$privileges_info is a php array, its form likes
        //$groups_info [$reponame] [$path]  [$privilege]  [$name]
        //$privilege could be '' , 'r' , 'w' , 'rw'
        //$name may be a groupname whose prefix is @ or a username
    }

    /*
     * e.g.:
     *
     * [groups]
     * admin = admin
     * proj1_developers = dev1 , dev2
     * proj2_developers = dev2
     * proj3_developers = dev3
     *
     */

    $file_content.='[groups]'."\n";
    foreach($groups_info as $group=>$userlist)
    {
        $file_content.=$group.' = ';
        $file_content.=implode(' , ', $userlist);
        $file_content.="\n";
    }

    $file_content.="\n";

    /*
     * e.g.:
     *
     * [:/]
     * * = r
     * @proj1_developers = rw
     *
     * [testproj1:/]
     * * = r
     * @proj1_developers = rw
     *
     * [testproj2:/]
     * * = r
     * @proj2_developers = rw
     * @proj3_developers = rw
     * 
     */

    foreach($privileges_info as $repo=>$pathlist)
    {
        foreach ($pathlist as $path=>$privilegelist)
        {
            $file_content.="[{$repo}:{$path}]\n";
            foreach($privilegelist as $privilege=>$namelist)
            {
                foreach($namelist as $name)
                {
                    $file_content.="{$name} = {$privilege}\n";
                }
            }
            $file_content.="\n";
        }
    }

    if(fwrite($file,$file_content)===false) //write file now
    {
        echo "File System Error";
        //TODO log
        exit;
    }

    flock($file, LOCK_UN);
    fclose($file);
}

?>
